package com.jilian.powerstation.utils;

import android.util.Log;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import lombok.val;

/**
 * 串口数据转换工具类
 * <p>
 * Created by Administrator on 2016/6/2.
 */

public class DataUtils {
    private static char[] hexArray = "0123456789ABCDEF".toCharArray();

    //-------------------------------------------------------

    // 判断奇数或偶数，位运算，最后一位是1则为奇数，为0是偶数

    public static int isOdd(int num) {
        return num & 1;
    }


    //-------------------------------------------------------

    //Hex字符串转int
    //06 01的byte字符转换时 (0106->262)
    //16进制字符串 转换成 10进制整数
    public static int HexToInt(String inHex) {

        BigInteger bi = new BigInteger(inHex, 16);
        int a=bi.intValue();
        System.out.println("ssssssssss"+a);
        if(a>32768){
            a = a-65536;
        }
        return a;
        //return Integer.parseInt(inHex, 16);
    }




    public static String IntToHex(int intHex) {
        return Integer.toHexString(intHex);

    }



    //-------------------------------------------------------

    //Hex字符串转byte

    public static byte HexToByte(String inHex) {
        try {
            return (byte) Integer.parseInt(inHex, 16);
        }
        catch (Exception e){
            return 0;
        }

    }

    /**
     * 16进制 - 转换  10进制
     * 两位
     * @param str
     * @return
     */
    public static String sixToten(String str) {
        String a = new BigInteger(str, 16).toString();
        if(a.length()==1){
            return "0"+a;
        }
        return a;

    }

    /**
     * 16进制 - 转换  10进制
     * @param str
     * @return
     */
    public static int sixTotenOne(String str) {
        return  new BigInteger(str, 16).intValue();

    }

    //-------------------------------------------------------

    //1字节转2个Hex字符

    public static String Byte2Hex(Byte inByte) {
        return String.format("%02x", new Object[]{inByte}).toUpperCase();
    }


    /**
     * 字节数组转字符串
     *
     * @param hexBytes 数组
     * @return 字符串
     */

    public static String hexBytesToString(byte[] hexBytes) {
        char[] hexChars = new char[hexBytes.length * 2];
        for (int j = 0; j < hexBytes.length; j++) {
            int v = hexBytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
    //-------------------------------------------------------

    //字节数组转转hex字符串

    public static String ByteArrToHex(byte[] inBytArr) {
        StringBuilder strBuilder = new StringBuilder();
        for (byte valueOf : inBytArr) {
            strBuilder.append(Byte2Hex(valueOf));
            strBuilder.append(" ");
        }
        return strBuilder.toString();
    }


    //-------------------------------------------------------

    //字节数组转转hex字符串，可选长度

    public static String ByteArrToHex(byte[] inBytArr, int offset, int byteCount) {
        StringBuilder strBuilder = new StringBuilder();
        int j = byteCount;
        for (int i = offset; i < j; i++) {
            strBuilder.append(Byte2Hex(Byte.valueOf(inBytArr[i])));
        }
        return strBuilder.toString();
    }


    //-------------------------------------------------------

    //转hex字符串转字节数组
    public static byte[] HexToByteArr(String inHex) {
        byte[] result;
        int hexlen = inHex.length();
        if (isOdd(hexlen) == 1) {
            hexlen++;
            result = new byte[(hexlen / 2)];
            inHex = "0" + inHex;
        } else {
            result = new byte[(hexlen / 2)];
        }

        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = HexToByte(inHex.substring(i, i + 2));
            j++;
        }
        return result;
    }


    /**
     * 按照指定长度切割字符串
     *
     * @param inputString 需要切割的源字符串
     * @param length      指定的长度
     * @return
     */

    public static List<String> getDivLines(String inputString, int length) {

        List<String> divList = new ArrayList<>();

        int remainder = (inputString.length()) % length;

        // 一共要分割成几段

        int number = (int) Math.floor((inputString.length()) / length);

        for (int index = 0; index < number; index++) {

            String childStr = inputString.substring(index * length, (index + 1) * length);

            divList.add(childStr);

        }

        if (remainder > 0) {

            String cStr = inputString.substring(number * length, inputString.length());

            divList.add(cStr);

        }

        return divList;

    }


    /**
     * 计算长度，两个字节长度
     *
     * @param val value
     * @return 结果
     */

    public static String twoByte(String val) {

        if (val.length() > 4) {

            val = val.substring(0, 4);

        } else {

            int l = 4 - val.length();

            for (int i = 0; i < l; i++) {

                val = "0" + val;

            }

        }

        return val;

    }


    /**
     * 校验和
     *
     * @param cmd 指令
     * @return 结果
     */

    public static String sum(String cmd) {

        List<String> cmdList = DataUtils.getDivLines(cmd, 2);

        int sumInt = 0;

        for (String c : cmdList) {

            sumInt += DataUtils.HexToInt(c);

        }

        String sum = DataUtils.IntToHex(sumInt);

        sum = DataUtils.twoByte(sum);

        cmd += sum;

        return cmd.toUpperCase();

    }

    public static String makeChecksum(String hexdata) {
        if (hexdata == null || hexdata.equals("")) {
            return "00";
        }
        hexdata = hexdata.replaceAll(" ", "");
        int total = 0;
        int len = hexdata.length();
        if (len % 2 != 0) {
            return "00";
        }
        int num = 0;
        while (num < len) {
            String s = hexdata.substring(num, num + 2);
            total += Integer.parseInt(s, 16);
            num = num + 2;
        }
        return hexInt(total);
    }

    /**
     *10进制 转换成 4位16进制
     * @param total
     * @return
     */
    public static String hexInt(int total) {
        int a = total / 256;
        int b = total % 256;
        if (a > 255) {
            return hexInt(a) + format(b);
        }
        String str =  format(a) + format(b);
        if(str.length()>4){
            str = str.substring(str.length()-4);
        }
        return str;

    }

    private static String format(int hex) {
        String hexa = Integer.toHexString(hex);
        int len = hexa.length();
        if (len < 2) {
            hexa = "0" + hexa;
        }
        return hexa;
    }


    public static String toHexString(int hex){
        //将其转换为十六进制并输出
        String strHex = Integer.toHexString(hex);
        System.out.println(strHex);
        return strHex;
    }


    public static void main(String[] args) {

        toHexString(187);

    }



}